home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 2010 April / PCWorld0410.iso / pluginy Firefox / 14642 / 14642.xpi / chrome / content / overlay.js < prev    next >
Text File  |  2009-10-14  |  11KB  |  325 lines

  1. /* Copyright 2009, Boomtango.com.  All Rights Reserved. */
  2. /* overlay.js
  3.  * Responsible for initializing app
  4.  */
  5.  
  6. Components.utils.import("resource://boomtango/app.js");
  7.  
  8. window.addEventListener("load", function() { boomtangoOverlay.init(document); }, false);
  9.  
  10. var boomtangoOverlay = {
  11.     cc: Components.classes,
  12.     ci: Components.interfaces,
  13.     
  14.     init: function() {
  15.         this.app = boomtangoApp;
  16.         this.app.init();
  17.         if(this.app.newUser){
  18.             this.doNewUser();
  19.             this.app.newUser = false;
  20.         }
  21.         this.browser = document.getElementById("content");
  22.  
  23.         gBrowser.tabContainer.addEventListener(
  24.             "TabSelect",
  25.             function(e){
  26.                 boomtangoOverlay.onTabChange(e);
  27.             },
  28.             false
  29.         );
  30.         gBrowser.tabContainer.addEventListener(
  31.             "TabOpen",
  32.             function(e){
  33.                 boomtangoOverlay.onTabOpen(e);
  34.             },
  35.             false
  36.         );
  37.  
  38.         var appcontent = document.getElementById("appcontent");   // browser
  39.         if(appcontent){
  40.             appcontent.addEventListener("DOMContentLoaded", function(e){
  41.                 boomtangoOverlay.onPageLoad(e);
  42.             }, true);
  43.         }
  44.         this.initPalette();
  45.         this.initGeolocation();
  46.         
  47.         this.app.firsttime = false;
  48.     },
  49.     initGeolocation: function(){
  50.                          /*
  51.         if(Components.classes["@mozilla.org/geolocation/provider;1"]){
  52.             var callback = {
  53.                 Update: function(pos){
  54.                     dump("Coords: " + pos.coords.latitude + ", " + pos.coords.longitude + "\n");
  55.                 },
  56.             };
  57.             var geoprovider =
  58.                 Cc["@mozilla.org/geolocation/provider;1"].getService(Ci.nsIGeolocationProvider);
  59.             geoprovider.startup();
  60.             geoprovider.watch(callback);
  61.         }
  62.         */
  63.     },
  64.     
  65.     initPalette: function() {
  66.         try {
  67.             var firefoxnav = document.getElementById("nav-bar");
  68.             var curSet = firefoxnav.currentSet;
  69.             if (curSet.indexOf("viewBoomtangoButtonIcon") == -1 && this.app.firsttime)
  70.             {
  71.                 var set;
  72.                 // Place the button before the urlbar
  73.                 if (curSet.indexOf("reload-button") != -1)
  74.                     set = curSet.replace(/reload-button/, "viewBoomtangoButtonIcon,reload-button");
  75.                 else  // at the end
  76.                     set = curSet + ",viewBoomtangoButtonIcon";
  77.                 firefoxnav.setAttribute("currentset", set);
  78.                 firefoxnav.currentSet = set;
  79.                 document.persist("nav-bar", "currentset");
  80.                 // If you don't do the following call, funny things happen
  81.                 try {
  82.                     BrowserToolboxCustomizeDone(true);
  83.                 }
  84.                 catch (e) { }
  85.             }
  86.         } catch(e) { }
  87.  
  88.         // if mac, change icon
  89.         if (navigator.platform.toLowerCase().indexOf('mac') > -1){ 
  90.             var button = document.getElementById("viewBoomtangoButtonIcon");
  91.             if(button){
  92.                 button.className += " macosup";
  93.             }
  94.         }
  95.     },
  96.     forEachTab:  function(func, funcThis){
  97.         var b = this.browser;
  98.         var len = b.browsers.length;
  99.         while(len--){
  100.             var curr = b.getBrowserAtIndex(len);
  101.             if(!curr.hasAttribute("boomtangoID")){
  102.                curr.setAttribute("boomtangoID",boomtangoApp.createTabID());
  103.             }
  104.             if(func.apply(this, [curr, curr.currentURI.spec])){
  105.                 return;
  106.             }
  107.         }
  108.     },
  109.     test: function(){
  110.         var panel = document.getElementById("bttest");
  111.         panel.openPopup(
  112.                 document.getElementById("urlbar"), "after_end", 0, 0, false, false);
  113.     },
  114.  
  115.     generateThumb: function(doc){
  116.         var win = doc.defaultView;
  117.         var canvas = document.getElementById("boomtangoThumbGen");
  118.         return boomtangoApp.generateThumb(win, canvas);
  119.     },
  120.  
  121.     ONEWEEK:  7 * 24 * 60 * 60 * 1000,
  122.     _initialOnLoadHasBeenCalled: false,
  123.     _currentLoadedURL: null,
  124.     onTabOpen: function(e){
  125.         boomtangoApp.onTabOpen(e.target.linkedBrowser);
  126.     },
  127.     _tabChangeTimer: null,
  128.     onTabChange: function(e){
  129.         // short circuit this if in private browsing mode
  130.         var pbcls =  Components.classes["@mozilla.org/privatebrowsing;1"];
  131.         if(pbcls){
  132.             var pbs = pbcls.getService(
  133.                     Components.interfaces.nsIPrivateBrowsingService); 
  134.             if(pbs.privateBrowsingEnabled){
  135.                 return;
  136.             }
  137.         }
  138.  
  139.         if(this._initialOnLoadHasBeenCalled){
  140.             var self = this;
  141.             if(self._tabChangeTimer){
  142.                 self._tabChangeTimer.cancel();
  143.                 self._tabChangeTimer = null;
  144.             }
  145.             var callback = {
  146.                 browserID: gBrowser.selectedTab.linkedBrowser.getAttribute("boomtangoID"),
  147.                 notify: function(){
  148.                     if(this.browserID == gBrowser.selectedTab.linkedBrowser.getAttribute("boomtangoID")){
  149.                         boomtangoApp.onTabChange(gBrowser.selectedTab.linkedBrowser);
  150.                         if(self._currID){
  151.                             self.handlePageUnload(self._currID, self._currDoc); 
  152.                         }
  153.                         if(boomtangoApp.inBlacklist(gBrowser.selectedTab.linkedBrowser.contentDocument.location.href)){
  154.                             return;
  155.                         }
  156.                         self.handlePageLoad(gBrowser.selectedTab.linkedBrowser.contentDocument);
  157.                     }
  158.                     
  159.                     if(self._tabChangeTimer){
  160.                         self._tabChangeTimer.cancel();
  161.                         self._tabChangeTimer = null;
  162.                     }
  163.                 }
  164.             };
  165.  
  166.             self._tabChangeTimer = Components.classes["@mozilla.org/timer;1"].
  167.                     createInstance(Components.interfaces.nsITimer);
  168.             self._tabChangeTimer.initWithCallback(
  169.                 callback,
  170.                 this.app.tabChangeTime * 1000,
  171.                 self._tabChangeTimer.TYPE_ONE_SHOT
  172.             );
  173.         }
  174.     },
  175.  
  176.     handlePageUnload: function(id, doc){
  177.         if(this._newthumbtimer){
  178.             this._newthumbtimer.cancel();
  179.             this._newthumbtimer = null;
  180.         }
  181.         if(this._newthumb && doc && doc.location){
  182.             var thumbrec = boomtangoApp.getThumbID(doc.location.href);
  183.             var d  =this.generateThumb(doc);
  184.             boomtangoApp.updateThumb(thumbrec.id, d);
  185.         }
  186.  
  187.         boomtangoApp.onPageUnload(id,doc);
  188.         this._currDoc = null;
  189.         this._currID = null;
  190.         this._newthumb = false;
  191.     },
  192.     handlePageLoad: function(doc){
  193.         // short circuit this if in private browsing mode
  194.         var pbcls =  Components.classes["@mozilla.org/privatebrowsing;1"];
  195.         if(pbcls){
  196.             var pbs = pbcls.getService(
  197.                     Components.interfaces.nsIPrivateBrowsingService); 
  198.             if(pbs.privateBrowsingEnabled){
  199.                 return;
  200.             }
  201.         }
  202.  
  203.         if(this._newthumbtimer){
  204.             this._newthumbtimer.cancel();
  205.             this._newthumbtimer = null;
  206.         }
  207.         var id = gBrowser.selectedBrowser.getAttribute("boomtangoID");
  208.         var url = doc.location.href;
  209.         var protocol = url.split(':')[0];
  210.         var thumb;
  211.         var newthumb = false;
  212.         var thumbrec = boomtangoApp.getThumbID(url);
  213.         var now = Date.now();
  214.         if(thumbrec.id < 0){    
  215.             thumb =this.generateThumb(doc);
  216.             newthumb = true;
  217.         } else if(boomtangoApp.refreshRequired(thumbrec.createtime)) {
  218.             var d  =this.generateThumb(doc);
  219.             boomtangoApp.updateThumb(thumbrec.id, d);
  220.             thumb = thumbrec.id;
  221.             newthumb = true;
  222.         } else {
  223.             thumb = thumbrec.id;
  224.         }
  225.  
  226.         // time to tell the app to load it
  227.         this._currDoc = doc;
  228.         this._currID = id;
  229.         this._newthumb = newthumb;
  230.  
  231.         var history = gBrowser.webNavigation.sessionHistory;
  232.         var currIndex = history.index;
  233.         var referrer = "";
  234.         if(currIndex){
  235.             var entry = history.getEntryAtIndex(currIndex - 1, false);
  236.             referrer = entry.URI.spec;
  237.         }
  238.  
  239.         boomtangoApp.onPageLoad(id, doc, thumb, referrer);
  240.         var self = this;
  241.         if(newthumb){
  242.             var callback = {
  243.                 notify: function(){
  244.                     var thumbrec = boomtangoApp.getThumbID(doc.location.href);
  245.                     var d  =self.generateThumb(doc);
  246.                     boomtangoApp.updateThumb(thumbrec.id, d);
  247.                     self._newthumbtimer = null;
  248.                     self._newthumb = newthumb;
  249.                 }
  250.             };
  251.             self._newthumbtimer = Components.classes["@mozilla.org/timer;1"].
  252.                     createInstance(Components.interfaces.nsITimer);
  253.             self._newthumbtimer.initWithCallback(
  254.                 callback,
  255.                 5 * 1000,
  256.                 self._newthumbtimer.TYPE_ONE_SHOT
  257.             );
  258.         }
  259.         doc.defaultView.addEventListener(
  260.             "unload", 
  261.             function(e){
  262.                 if(self._currID == id){
  263.                     self.handlePageUnload(id, doc);
  264.                 }
  265.             }, 
  266.             true
  267.         );
  268.  
  269.     },
  270.     onPageLoad: function(e) {
  271.         var doc = e.originalTarget; 
  272.         var url = doc.location.href;
  273.         var self = this;
  274.  
  275.         if(e.originalTarget.nodeName == "#document"){
  276.             var serpquery = /http:\/\/www\.google\..+\/search.*[?&]q=([^&]+)/.exec(url);
  277.             if(serpquery){
  278.                 var query = serpquery[1];
  279.                 boomtangoApp.doSerp(b, doc, query);
  280.             }
  281.         }
  282.         // if this is just an iframe, we don't want to know about it
  283.         if (e.originalTarget instanceof HTMLDocument && 
  284.             e.originalTarget.defaultView.frameElement) {
  285.             return;
  286.         }
  287.  
  288.         // let our tab watcher know to start watching
  289.         if(!this._initialOnLoadHasBeenCalled){
  290.             var b = this.browser;
  291.             var len = b.browsers.length;
  292.             while(len--){
  293.                 var curr = b.getBrowserAtIndex(len);
  294.                 if(!curr.hasAttribute("boomtangoID")){
  295.                     curr.setAttribute("boomtangoID",boomtangoApp.createTabID());
  296.                 }
  297.             }
  298.             this._initialOnLoadHasBeenCalled = true;
  299.         }
  300.  
  301.  
  302.         if(boomtangoApp.inBlacklist(url)){
  303.             return;
  304.         }
  305.         if(gBrowser.selectedBrowser.contentDocument.location.href == url){
  306.             this.handlePageLoad(doc);
  307.         }
  308.  
  309.     },
  310.     doNewUser: function(){
  311.         window.openDialog(
  312.             "chrome://boomtango/content/import.xul",
  313.             "Boomtango",
  314.             "chrome,centerscreen,dialog,close=yes,alwaysRaised=no"
  315.         );
  316.     },
  317.     openSettings: function() {
  318.         window.openDialog('chrome://boomtango/content/settings.xul', 'settingsDlg', 'dialog=yes,resize=no,toolbar,centerscreen').focus();
  319.     },
  320.     openHistory: function(){
  321.         this.app.openHistory(this.browser);
  322.     },
  323. };
  324.  
  325.